<template>
  <nav class="top-navbar">
    <div class="navbar-container">
      <!-- Logo区域 -->
      <div class="logo-section">
        <img src="/img/6.png" alt="崩坏3" class="logo" />
        <span class="logo-text">崩坏3</span>
      </div>
      
      <!-- 导航菜单 -->
      <div class="nav-menu">
        <a href="#" class="nav-item" :class="{ active: activeItem === 'home' }" @click.prevent="setActive('home')">
          首页
        </a>
        <a href="#" class="nav-item" :class="{ active: activeItem === 'news' }" @click.prevent="setActive('news')">
          新闻
        </a>
        <a href="#" class="nav-item" :class="{ active: activeItem === 'character' }" @click.prevent="setActive('character')">
          角色
        </a>
        <a href="#" class="nav-item" :class="{ active: activeItem === 'stage' }" @click.prevent="setActive('stage')">
          场景
        </a>
        <a href="#" class="nav-item" :class="{ active: activeItem === 'weapon' }" @click.prevent="setActive('weapon-display')">
          武器
        </a>
        <a href="#" class="nav-item" :class="{ active: activeItem === 'media' }" @click.prevent="setActive('media')">
          视听中心
        </a>
        <!-- 管理系统菜单（仅管理员可见） -->
        <div v-if="userStore.isAdmin" class="admin-menu">
          <a href="#" class="nav-item admin-nav" :class="{ active: activeItem === 'admin' }" @click.prevent="toggleAdminMenu">
            管理系统
            <svg class="dropdown-icon" :class="{ rotated: showAdminMenu }" width="12" height="12" viewBox="0 0 24 24" fill="none">
              <path d="M6 9L12 15L18 9" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
          </a>
          <div v-if="showAdminMenu" class="admin-dropdown">
            <a href="#" class="admin-menu-item" @click.prevent="handleAdminNavigation">
              用户管理
            </a>
            <a href="#" class="admin-menu-item" @click.prevent="handleHeroManagement">
              英雄管理
            </a>
            <a href="#" class="admin-menu-item" @click.prevent="handleSceneManagement">
              场景管理
            </a>
            <a href="#" class="admin-menu-item" @click.prevent="handleWeaponManagement">
              武器管理
            </a>
            <a href="#" class="admin-menu-item" @click.prevent="handleSkillManagement">
              技能管理
            </a>
          </div>
        </div>
      </div>
      
      <!-- 用户操作区域 -->
      <div class="user-actions">
        <!-- 未登录状态 -->
        <div v-if="!userStore.isLoggedIn" class="auth-buttons">
          <button class="login-btn" @click="showLogin">
            登录
          </button>
          <button class="register-btn" @click="showRegister">
            注册
          </button>
        </div>
        
        <!-- 已登录状态 -->
        <div v-else class="user-info">
          <div class="user-avatar">
            <img v-if="userStore.userInfo?.userAvatar" 
                 :src="userStore.userInfo.userAvatar" 
                 :alt="userStore.userInfo.userName" />
            <div v-else class="default-avatar">
              {{ (userStore.userInfo?.userName || userStore.userInfo?.userAccount || 'U').charAt(0).toUpperCase() }}
            </div>
          </div>
          <div class="user-details">
            <span class="user-name">{{ userStore.userInfo?.userName || userStore.userInfo?.userAccount }}</span>
            <span v-if="userStore.isAdmin" class="user-role admin">管理员</span>
            <span v-else class="user-role">用户</span>
          </div>
          <div class="user-menu">
            <button class="menu-btn" @click="toggleUserMenu">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none">
                <path d="M6 9L12 15L18 9" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              </svg>
            </button>
            <div v-if="showUserMenu" class="dropdown-menu">
              <a href="#" class="menu-item" @click.prevent="handleProfile">
                <span>个人中心</span>
              </a>
           
              <div class="menu-divider"></div>
              <a href="#" class="menu-item logout" @click.prevent="handleLogout">
                <span>退出登录</span>
              </a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </nav>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'

const emit = defineEmits(['navigate', 'show-login', 'show-register', 'logout'])

const route = useRoute()
const router = useRouter()
const activeItem = ref('home')
const showUserMenu = ref(false)
const showAdminMenu = ref(false)
const userStore = useUserStore()

// 根据当前路由设置活跃项
const updateActiveFromRoute = () => {
  const path = route.path
  if (path === '/') {
    activeItem.value = 'home'
  } else if (path === '/news') {
    activeItem.value = 'news'
  } else if (path === '/character') {
    activeItem.value = 'character'
  } else if (path === '/stage') {
    activeItem.value = 'stage'
  } else if (path === '/weapon-display' || path === '/weapon') {
    activeItem.value = 'weapon'
  } else if (path === '/media') {
    activeItem.value = 'media'
  } else if (path === '/admin') {
    activeItem.value = 'admin'
  }
}

// 监听路由变化
watch(() => route.path, updateActiveFromRoute, { immediate: true })

const setActive = (item) => {
  activeItem.value = item
  // 直接使用路由跳转
  router.push(`/${item === 'home' ? '' : item}`)
}

const showLogin = () => {
  emit('show-login')
}

const showRegister = () => {
  emit('show-register')
}

const toggleUserMenu = () => {
  showUserMenu.value = !showUserMenu.value
}

const toggleAdminMenu = () => {
  showAdminMenu.value = !showAdminMenu.value
}



const handleAdminNavigation = () => {
  showAdminMenu.value = false
  router.push('/admin')
}

const handleHeroManagement = () => {
  showAdminMenu.value = false
  router.push('/hero-management')
}

const handleSceneManagement = () => {
  showAdminMenu.value = false
  router.push('/scene-management')
}

const handleWeaponManagement = () => {
  showAdminMenu.value = false
  router.push('/weapon-management')
}

const handleSkillManagement = () => {
  showAdminMenu.value = false
  router.push('/skill-management')
}

const handleProfile = () => {
  showUserMenu.value = false
  router.push('/profile')
}

const handleSettings = () => {
  showUserMenu.value = false
  router.push('/settings')
}



const handleLogout = () => {
  showUserMenu.value = false
  emit('logout')
}

// 点击外部关闭菜单
const handleClickOutside = (event) => {
  if (!event.target.closest('.user-menu')) {
    showUserMenu.value = false
  }
  if (!event.target.closest('.admin-menu')) {
    showAdminMenu.value = false
  }
}

// 监听点击事件
if (typeof window !== 'undefined') {
  document.addEventListener('click', handleClickOutside)
}
</script>

<style scoped>
.top-navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 70px;
  background: transparent;
  /* backdrop-filter: blur(10px); */
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  z-index: 1000;
}

.navbar-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo {
  height: 40px;
  width: 40px;
}

.logo-text {
  font-size: 24px;
  font-weight: bold;
  color: #fff;
  background: linear-gradient(45deg, #e8f4f8, #f0f8ff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.nav-menu {
  display: flex;
  gap: 30px;
}

.nav-item {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  font-size: 16px;
  font-weight: 500;
  padding: 8px 16px;
  border-radius: 20px;
  transition: all 0.3s ease;
  position: relative;
}

.nav-item:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
  transform: translateY(-2px);
}

.nav-item.active {
  color: turquoise;
  background: linear-gradient(45deg, #2c3e50, #34495e);
  box-shadow: 0 4px 15px rgba(44, 62, 80, 0.3);
}

.nav-item.active::before {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 6px;
  height: 6px;
  background: #87ceeb;
  border-radius: 50%;
  box-shadow: 0 0 10px #87ceeb;
}

.user-actions {
  display: flex;
  gap: 15px;
}

.login-btn,
.register-btn {
  padding: 8px 20px;
  border: none;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.login-btn {
  background: transparent;
  color: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.login-btn:hover {
  color: #fff;
  border-color: #fff;
  background: rgba(255, 255, 255, 0.1);
}

.register-btn {
  background: linear-gradient(45deg, #e8f4f8, #f0f8ff);
  color: #333;
  border: 1px solid transparent;
}

.register-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(232, 244, 248, 0.4);
}

/* 用户信息样式 */
.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 25px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
}

.user-info:hover {
  background: rgba(255, 255, 255, 0.15);
  transform: translateY(-1px);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(45deg, #e8f4f8, #f0f8ff);
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.default-avatar {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #333;
  font-weight: bold;
  font-size: 14px;
}

.user-details {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.user-name {
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  line-height: 1;
}

.user-role {
  color: rgba(255, 255, 255, 0.7);
  font-size: 12px;
  line-height: 1;
}

.user-role.admin {
  color: #ffd700;
  font-weight: 500;
}

.user-menu {
  position: relative;
}

.menu-btn {
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.8);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.menu-btn:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  min-width: 150px;
  background: rgba(0, 0, 0, 0.9);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 8px 0;
  margin-top: 8px;
  z-index: 1001;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.menu-item {
  display: block;
  padding: 10px 16px;
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s ease;
  cursor: pointer;
}

.menu-item:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.menu-item.logout {
  color: #ff6b6b;
}

.menu-item.logout:hover {
  background: rgba(255, 107, 107, 0.1);
  color: #ff6b6b;
}

.menu-divider {
  height: 1px;
  background: rgba(255, 255, 255, 0.1);
  margin: 8px 0;
}

/* 管理系统菜单样式 */
.admin-menu {
  position: relative;
}

.admin-nav {
  display: flex;
  align-items: center;
  gap: 5px;
}

.dropdown-icon {
  transition: transform 0.3s ease;
}

.dropdown-icon.rotated {
  transform: rotate(180deg);
}

.admin-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 120px;
  background: rgba(0, 0, 0, 0.9);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 8px 0;
  margin-top: 8px;
  z-index: 1001;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.admin-menu-item {
  display: block;
  padding: 10px 16px;
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s ease;
}

.admin-menu-item:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}



/* 响应式设计 */
@media (max-width: 768px) {
  .navbar-container {
    padding: 0 15px;
  }
  
  .nav-menu {
    gap: 15px;
  }
  
  .nav-item {
    font-size: 14px;
    padding: 6px 12px;
  }
  
  .logo-text {
    font-size: 20px;
  }
  
  .user-actions {
    gap: 10px;
  }
  
  .login-btn,
  .register-btn {
    padding: 6px 15px;
    font-size: 12px;
  }
  
  .user-info {
    gap: 8px;
    padding: 6px 12px;
  }
  
  .user-avatar {
    width: 28px;
    height: 28px;
  }
  
  .default-avatar {
    font-size: 12px;
  }
  
  .user-name {
    font-size: 12px;
  }
  
  .user-role {
    font-size: 10px;
  }
  
  .dropdown-menu {
    min-width: 130px;
  }
  
  .menu-item {
    padding: 8px 12px;
    font-size: 12px;
  }
}
</style>